<template>
  <view>
    <!-- 奖励弹窗 -->
    <effect-first ref="effectFirst" @close="handleClose" @loginSuccess="loginSuccess" @openFinsh="handleShowFinish" />
    <!-- 零钱页面 -->
    <effectList ref="effectList" @showFinish="handleShowFinish" @updateCurrentAmount="finishChange" v-if="loginFlag">
    </effectList>
    <!-- 完成页面 -->
    <effectFinish ref="effectFinish" @closeFinsh="finishChange"></effectFinish>

    <!-- 提示 -->
    <animationError
      ref="animationError"
      :showImageUrl="showImageUrl" :showTtitle="showTtitle"
      :showText="errortext"
      @ClickGo="ClickGo"
    >
    </animationError>

    <!-- 领取 -->
    <animationReward ref="animationReward" showText="去个人中心查看" @getClick="getClick"></animationReward>
  </view>
</template>

<script>
import effectFirst from '@/components/effect-component/effect-first.vue'
import effectList from '@/components/effect-component/effect-list.vue'
import effectFinish from '@/components/effect-component/effect-finish.vue'
import animationReward from '@/components/effect-component/animation-reward.vue'
import newUserRewardApi from '@/api/newUserReward.js'
import animationError from '@/components/effect-component/animation-error.vue'
import store from '@/public/store.js'
const app = getApp();

export default {
  components: {
    effectFirst,
    effectList,
    effectFinish,
    animationReward,
    animationError
  },
  data() {
    return {
      loginFlag: false,
      my_reward: [],
      tandianInfo: {},
      BestImgUrl: app.globalData.imgurl,
      myInfo: {},
      errortext: '领取失败',
      showTtitle: '领取失败',
      showImageUrl: `${app.globalData.imgurl}/image_684.png` ,
    }
  },
  onLoad() {

  },
  mounted() {
    console.log('effect-index')
    let iftoken = uni.getStorageSync('token')
    if (iftoken) {
      this.loginFlag = true
    }

    // this.$refs.effectFirst.showPopup()
  },
  methods: {

    // 结束动画显示礼盒
    finishChange() {
      this.$refs.effectFirst.showIcon = true
      this.updateMoney()
    },
    finishReward(type) {
      // console.log('111')
      if (type == 3 && this.tandianInfo.is_receive == 1) {
        this.$refs.animationReward.showUp()
      }
    },
    ClickGo(flag){
      if(flag){
        uni.switchTab({
          url: '/pages/WoDe/WoDe'
        })
      }
    },
    // 获取奖励
    getClick(type) {
      const {tandianInfo, myInfo} = this
      if (type == 3) {
        const data = {
          id: tandianInfo.id,
        }
        // increaseBalance();
        // return
        newUserRewardApi.setMyReward(data).then(res => {
          this.errortext = '去个人中心查看'
          this.showTtitle = '领取成功'
          this.showImageUrl= `${app.globalData.imgurl}/image_688@2x.png` ,
          this.$refs.animationError.showUp()
          // return
          const lastMonye = myInfo.difference_amount - tandianInfo.amount
          // console.log('lastMonye', lastMonye)
          store.commit('setWithdrawalTarget', lastMonye)
        }).catch(err => {
          // console.log('errrr',err)
          this.showTtitle = '领取失败'
          this.errortext = err.data.msg
          this.showImageUrl = `${app.globalData.imgurl}/image_684.png`
          this.$refs.animationError.showUp()
        })
      }
    },
    // 登录成功
    loginSuccess() {
      this.loginFlag = true
    },
    // 更新金额
    updateMoney() {
      // this.$refs.effectFirst.withdrawalTarget = this.$refs.effectList.totalAmount
    },
    handleShowFinish() {
      //获取省份判断是否显示旧版提现入口
      let CityName = uni.getStorageSync('CityName')
      let Province = uni.getStorageSync('Province')
      const submitData = {
        province: Province,
        city: CityName
      }
      newUserRewardApi.getNewUserReward(submitData).then(res => {
        const data = res.data
        const { result, code } = data
        if (code == 200 && result && result.length) {
          result.forEach(item => {
            if (item.type === 3) {
              this.tandianInfo = item
              if (item.is_receive === 1) {
                // console.log('已领取')
                this.finishReward(item.type)
                return
              }
              // console.log('this.$refs', this.$refs)
              this.$refs.effectFinish.showUp()
              // this.$refs.animationErrort.showUp()
            }
          })
        }
      })
      // this.showFinishPopup = true;
      // this.$refs.effectFinish.showUp()
    },
    //获取我的奖励信息
    handleClose() {

      newUserRewardApi.getMyReward().then(res => {//我的奖励信息接口
        const data = res.data
        const { result, code } = data
        console.log('这是我', result)
        this.myInfo = result
        this.my_reward = result.my_reward

        setTimeout(() => {
          // changett
          // this.$refs.effectList.showUp();
          // return
          /*this.$api.GetuserInfo().then(res => {
            console.log('我的信息', res.data.result)
            let userInfo = res.data.result
            const regex = /^1[3-9]\d{9}$/
            if(userInfo){
              this.$refs.effectList.showUp();
            }
            if(userInfo.username && regex.test(userInfo.username)){
              this.handleShowFinish()
            }else {

            }
          })*/

          if (!result.my_reward.length) {
            this.$refs.effectList.showUp();
          } else if (result.my_reward.length < 2) {
            this.$refs.effectList.getEffectBind()
          } else {
            // this.$refs.effectList.getEffectBind()
            this.handleShowFinish()
          }
        }, 500)
      })
    },
  }
}
</script>

<style></style>